package dLogistic.domain.model.Paczka;

import dLogistic.infrastructure.persistence.hibernate.HibernateRepository;
import dLogistic.infrastructure.persistence.hibernate.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.List;

/**
 * Created by Przemek on 2014-10-24.
 */

public class PaczkaRepository extends HibernateRepository {
    protected static SessionFactory factory;

    public PaczkaAggr znajdz(String uuid){
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();

        Transaction transaction = session.beginTransaction();
        String sql = "select * from Paczka where uuid = :uuid";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(PaczkaAggr.class);
        query.setParameter("uuid", uuid);
        List results = query.list();
        transaction.rollback();
        if (!results.isEmpty()){
            return (PaczkaAggr) results.get(0);
        }
        else {
            return null;
        }

    }


    public void zapisz(PaczkaAggr paczka) {
        /*
        try{
            factory = new Configuration().configure("/resources/hibernate.cfg.xml").buildSessionFactory();
        }catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        */
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.saveOrUpdate(paczka);
        transaction.commit();
    }

    public List<PaczkaAggr> znajdzWszystkie() {
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();

        Transaction transaction = session.beginTransaction();
        String sql = "select * from Paczka";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(PaczkaAggr.class);
        transaction.rollback();
        return query.list();


    }

    public JSONArray zwrocListAll(){
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        String sql = "select * from Paczka";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(PaczkaAggr.class);

        List results = query.list();
        transaction.rollback();
        JSONArray ja = new JSONArray();
        for (Integer i = 0; i < results.size(); i++) {
            PaczkaAggr paczkaAggr = (PaczkaAggr) results.get(i);
            JSONObject obj = new JSONObject();
            obj.put("uuid", paczkaAggr.uuid());
            obj.put("nadawca", paczkaAggr.nadawca());
            obj.put("adresat", paczkaAggr.adresat());
            ja.add(obj);
        }
        return ja;
    }

    //możliwość zapisania paczki


    //wyszukiwacze paczek
    //- po id klienta
    //- po id paczki

}
